<template lang="jade">
div
  h-loader-full(url='loader',:setdata='false')
  m-title(icon='add',name='添加题目')
  table.single

    mf-row(name='所属培训项')
      select(v-model='data.sid',placeholder='请选择培训项')
        option(value='', selected) 请选择培训项
        option(v-for='(s,i) in school', :value='s.id') {{s.name}}

    mf-row(name='题目类型')
      select(v-model='data.type',placeholder='请选择题目类型')
        option(value='', selected) 请选择题目类型
        option(v-for='(s,i) in type_arr', :value='s.id') {{s.name}}

    mf-row(name='问题')
      input(type = 'text',v-model='data.question',placeholder='请填写问题')

    mf-row(name='题目图片')
      mf-upload(uploadid='img', amount='1', project='xiongan', category='logo', :data='data.img', @upload_data='text',style='marginTop:20px')
      p(style='marginBottom:0px') 


    mf-row(:name='"答案"+(key+1)', v-for='(item,key) in answer')
      input(type='text', v-model='answer[key]', placeholder='请填写答案') 
      label(style="cursor: pointer; color:blue", @click="addanswer") [点击添加更多]
      label(style="cursor: pointer; color:red", @click="delanswer(key)") [删除]
      label(style="cursor: pointer; color:green", @click="setrightanswer(key)") [设为正确答案]

    mf-row(name='正确答案')
      input(type = 'text',v-model='data.rightanswer',placeholder='请选择正确答案', readonly="readonly")

    mf-row(name='是否为重点题目')
      select(v-model='data.important',placeholder='请选择是否为重点题目')
        option(value='', selected) 请选择是否为重点题目
        option(v-for='(s,i) in important_arr', :value='s.id') {{s.name}}


    mf-row
      button(@click='submitinfo') 添加
</template>

<script>
import { setTimeout } from 'timers';
export default {
	data () {
		return {
      loader:{
        school:'cultivate/school/list',
      },
      answer:[''],
      school: [],
      data:{
        sid:'',
        question:'',
        answer:'',
        rightanswer:'',
        important:'',
        img:[],
        type:"",
      },
      important_arr:[
        {
          id:0,
          name:"非重点题"
        },
        {
          id:1,
          name:"重点题"
        }
      ],
      type_arr:[
        {
          id:10,
          name:"判断"
        },
        {
          id:20,
          name:"单选"
        }
      ],


		}
	},
  watch:{
  },
	methods: {
    loadAfter(res){
      this.school = res.school[0]
    },
    
    text(data){
      this.data.img = data;
    },


    addanswer(){
      this.answer.push(''); 
    },
    delanswer(key){
      this.answer.splice(key,1);
    },
    setrightanswer(key){
      this.data.rightanswer = this.answer[key];
    },
    submitinfo() {



      this.data.answer = this.answer.join("+|+|");
      for(var key in this.data){
        if(key == "img"){
          continue;
        }


        if((this.data[key]+"") === ""){
          alert('所有均不能为空');
          return;
        }
      }

      if(this.data.img.length == 0){
        this.data.img = "";
      }
      
      this.$ajax.post('question/add/question', this.data ,(res)=>{
        this.$msg.success(res.message);
        setTimeout(()=>{
          window.location.href = window.location.href;
        },1000);
      },(res)=>{
        this.$msg.error(res.message)
      })
    },
	}
}
</script>
